package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.domain.Travel;
import cn.wolfcode.luowowo.acticle.query.HomeQueryObject;
import cn.wolfcode.luowowo.acticle.service.ITravelService;
import cn.wolfcode.luowowo.cache.prefix.UserInfoKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/mine")
public class MyHomeController {


    @Reference
    private ITravelService travelService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private ITravelStatisRedisService travelStatisRedisService;

    /**
     * 我的窝首页,查询别人的窝
     *
     * @param model
     * @return
     */
    @RequestMapping("/home")
    @RequireLogin
    public String home(Model model, @UserParam UserInfo userInfo, Long sid) {
        if(userInfo.getId() == sid){
            two2one(model, userInfo, userInfo.getId(), -1);
        }else {
            if (sid == null) {
                //共享热门游记到首页
                two2one(model, userInfo, userInfo.getId(), -1);
            } else {
                //共享热门游记到首页
                UserInfo user = userInfoService.get(sid);
                two2one(model, user, sid, 1);
            }
        }
        return "myhome/homepage";
    }
    /**
     * 方法抽离
     *
     * @param model
     * @param userInfo
     * @param id
     * @param a
     */
    private void two2one(Model model, UserInfo userInfo, Long id, int a) {
        //查询我所有的关注
        List<UserInfo> idols = userInfoService.getFromId(id);
        userInfo.setIdols(idols);
        //查询我所有的粉丝
        List<UserInfo> fans = userInfoService.getFans(id);
        userInfo.setFans(fans);
        List<Travel> travels = travelService.selectByNewTop6(id);
        model.addAttribute("a", a);
        model.addAttribute("idosnums", idols.size());
        model.addAttribute("fansnums", fans.size());
        model.addAttribute("travels", travels);
        model.addAttribute("count", travelService.count(id));
        model.addAttribute("userInfo", userInfo);
    }

    /**
     * 我的游记页面
     *
     * @return 跳转到我的游记页面
     */
    @RequestMapping("mytravelnotes")
    @RequireLogin
    public String mytravelnotes(Model model, @UserParam UserInfo userInfo,HomeQueryObject qo,Long id) {
        if(userInfo.getId() == id){
            travelnotes(model,userInfo,qo,1);
        }else {
            if(id != null){
                UserInfo user = userInfoService.get(id);
                travelnotes(model,user,qo,-1);
            }else {
                travelnotes(model,userInfo,qo,1);
            }
        }
        return "myhome/mytravelnotes";
    }

    /**
     * 游记代码抽离
     */
    private void travelnotes(Model model,UserInfo userInfo,@ModelAttribute("qo") HomeQueryObject qo,int a){
        //查询所有的游记并分页
        model.addAttribute("pageInfo", travelService.queryForPage(qo, userInfo.getId()));
        //共享总数量
        model.addAttribute("count", travelService.count(userInfo.getId()));
        //共享当前用户信息
        model.addAttribute("userInfo", userInfo);
        model.addAttribute("a",a);
    }

    /**
     * 我的点评
     *
     * @return
     */
    @RequestMapping("review")
    @RequireLogin
    public String review(@UserParam UserInfo userInfo, Model model) {
        model.addAttribute("userInfo", userInfo);
        model.addAttribute("a",1);
        return "myhome/review";
    }

    /**
     * 我的收藏
     *
     * @return
     */
    @RequestMapping("travelcollection")
    @RequireLogin
    public String travelcollection(@UserParam UserInfo userInfo, Model model,Long id) {
        if(userInfo.getId() == id){
            favor(userInfo,model,1);
        }else {
            if(id == null){
                favor(userInfo,model,1);
            }else {
                UserInfo user = userInfoService.get(id);
                favor(user,model,-1);
            }
        }
        return "myhome/travelcollection";
    }
    /**
     * 我的收藏代码抽离
     */
    public void favor(UserInfo userInfo,Model model,int a){
        model.addAttribute("userInfo", userInfo);
        //获取当前用户收藏的游记
        List<StrategyDetail> strategyDetails = strategyStatisRedisService.getStrategyDetails(userInfo.getId());
        model.addAttribute("details",strategyDetails);
        //获取当前用户收藏的游记
        List<Travel> travels = travelStatisRedisService.getTravels(userInfo.getId());
        model.addAttribute("travels",travels);
        model.addAttribute("a",a);
    }

    /**
     * 设置
     *
     * @return
     */
    @RequestMapping("setting")
    @RequireLogin
    public String setting(@UserParam UserInfo user, Model model) {
        //当别的页面跳转到这时执行查询语句共享当前用户的信息
        model.addAttribute("userInfo", user);
        return "myhome/setting";
    }

    @RequestMapping("update")
    @RequireLogin
    public String update(HttpSession session,HttpServletResponse resp,Model model,UserInfo userInfo) {
        String token = userInfoService.update(userInfo);
        updateRedis(session,resp,model,token);
        return "myhome/setting";
    }
    @RequestMapping("updatePassword")
    @ResponseBody
    @RequireLogin
    public Object updatePassword(HttpSession session,HttpServletResponse resp,Model model,String newpassword,Long id){
        String token = userInfoService.updatePassWord(newpassword,id);
        updateRedis(session,resp,model,token);
        return new AjaxResult();
    }
    //跟新redis里面的数据
    private void updateRedis(HttpSession session,HttpServletResponse resp,Model model,String token){
        session.removeAttribute("userInfo");
        CookieUtil.addCookie(resp, UserInfoKeyPrefix.USER_INFO_COOKIE_NAME, token);
        session.setAttribute("userInfo", userInfoRedisService.getUserInfoByToken(UserInfoKeyPrefix.USER_INFO_TOKEN, token));
        UserInfo user = userInfoRedisService.getUserInfoByToken(UserInfoKeyPrefix.USER_INFO_TOKEN, token);
        model.addAttribute("userInfo", user);
    }


    @Value("${file.path}")
    private String filePath;
    @RequestMapping("uploadImg")
    @RequireLogin
    @ResponseBody
    public Object uploadImg(HttpServletResponse resp, HttpSession session,MultipartFile files,Long uid,Model model) {
        String upload = UploadUtil.upload(files, filePath);
        String token = userInfoService.updateheadImg(upload,uid);
        updateRedis(session,resp,model,token);
        return upload;
    }
}
